package com.qk.mangement.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.activity.ActivityDTO;
import com.qk.common.PageResult;
import com.qk.common.enums.BizEnum;
import com.qk.common.excption.BizException;
import com.qk.entity.Activity;
import com.qk.mangement.mapper.ActivityMapper;
import com.qk.mangement.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * Date:2025/10/11
 * Author:zxc
 * Description
 */
@Service
public class ActivityServiceImpl  extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public PageResult<Activity> queryListByPage(ActivityDTO activityDTO) {
//        PageInfo<Activity> pageInfo = PageHelper.startPage(activityQueryListByPageDto.getPage(), activityQueryListByPageDto.getPageSize())
//                .doSelectPageInfo(() -> activityMapper.queryPage(activityQueryListByPageDto));
//        return PageResult.<Activity>builder().total(pageInfo.getTotal()).rows(pageInfo.getList()).build();
        Page<Activity> page = new Page<>(activityDTO.getPage(), activityDTO.getPageSize());
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(activityDTO.getChannel() != null, Activity::getChannel, activityDTO.getChannel())
                .eq(activityDTO.getType() != null, Activity::getType, activityDTO.getType())
                .gt(activityDTO.getStatus() != null && activityDTO.getStatus() == 1, Activity::getStartTime, LocalDateTime.now())
                .le(activityDTO.getStatus() != null && activityDTO.getStatus() == 2, Activity::getStartTime, LocalDateTime.now())
                .ge(activityDTO.getStatus() != null && activityDTO.getStatus() == 2, Activity::getEndTime, LocalDateTime.now())
                .lt(activityDTO.getStatus() != null && activityDTO.getStatus() == 3, Activity::getEndTime, LocalDateTime.now())
                .orderByDesc(Activity::getUpdateTime);
        IPage<Activity> iPage = activityMapper.selectPage(page, queryWrapper);
        return PageResult.<Activity>builder().total(Long.valueOf(iPage.getTotal()).intValue()).rows(iPage.getRecords()).build();
    }

    @Override
    public void deleteById(Integer id) {
        if (Objects.isNull(id)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        activityMapper.deleteById(id);
    }

    @Override
    public void addActivity(Activity activity) {
        boolean hasNull = BeanUtil.hasNullField(activity, "id", "discount", "voucher", "createTime", "updateTime");
        if (hasNull){
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.addCourse(activity);
    }

    @Override
    public Activity queryById(Integer id) {
        if (Objects.isNull(id)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        Activity activity = activityMapper.queryById(id);
        return activity;
    }

    @Override
    public void updateActivity(Activity activity) {
        boolean hasNull = BeanUtil.hasNullField(activity,  "discount", "voucher", "createTime", "updateTime");
        if (hasNull){
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.updateActivity(activity);
    }

    @Override
    public List<Activity> queryByType(Integer type) {
        if (Objects.isNull(type)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        List<Activity> activitiesList = activityMapper.queryByType(type);
        return activitiesList;
    }

}
